public class BinarySearch<T extends Comparable<T>> {

    //二分查找arr，找到则返回所在索引，否则返回-1
    public int binarySearch(T[] arr, T val) {
        int start = 0;
        int end = arr.length - 1;
        int mid = (start  + end)/2;
        while (start <= end) {
            if (val.equals(arr[mid])) {
                return mid;
            } else if (arr[mid].compareTo(val) > 0) {
                //向左找
                end = mid - 1;
            }else {
                //向右找
                start = mid + 1;
            }
            System.out.println("在区间["+start + "," + end + "] 查找");
            mid = (start + end) / 2;
        }
        return -1;
    }
    public static void main(String[] args) {
        Integer[] arr = {1, 3, 4, 6, 7, 9, 12, 35, 87, 4234};
        System.out.println(new BinarySearch<Integer>().binarySearch(arr, 13));
    }
}
